public class MySignalLinkedList {

    //节点类
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }



    //返回倒数第k个节点的值，该方法遍历了链表两次
    public int kthToLast(int k){
        if (k < 0 || k > size()){
            return -1;
        }
        //先让fast走k-1步
        ListNode fast = head;
        ListNode slow = head;
        int count = 0;
        while (count != k-1){
            fast = fast.next;
            count ++;
        }
        while(fast.next != null){
            slow = slow.next;
            fast = fast.next;
        }
        return slow.val;

    }
    //编写代码，以给定值x为基准将链表分割成两部分，所有⼩于x的结点排在⼤于或等于x的结点之前
    //x为值，不是节点位置
    public ListNode Partition(ListNode head,int  x){
        //定义两个链表的四个节点
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = head;
        while(cur != null){
            //在第一个链表中插入数据
            if (cur.val < x){
                if (bs == null){
                    bs = be = cur;
                }else{
                    be.next = cur;
                    be = be.next;

                }
            }else {
                //在第二个链表中插入数据
                if (as == null){
                    as = ae = cur;
                }else{
                    ae.next = cur;///更新当前尾节点
                    ae = ae.next;//更新尾节点，为了下一个节点的尾插
                }
            }
            cur = cur.next;

        }
        if (bs == null){
            return as;
        }
        if (as == null){
            ae.next = null;//链表尾节点next要置空
        }
        be.next = as;
        return bs;

    }

    //返回倒数第k个节点的值，只遍历一遍链表
    public int kthToLast2(int k){
        if (k < 0){
            return -1;
        }
        //先让fast走k-1步
        ListNode fast = head;
        ListNode slow = head;
        int count = 0;
        while (count != k-1){
            if (head == null){
                return -1;
                //head等于看空，说明k >= size()+1,也就是走过了
            }
            fast = fast.next;
            count ++;
        }
        while(fast.next != null){
            slow = slow.next;
            fast = fast.next;
        }
        return slow.val;

    }
    //链表逆置
    public ListNode reverseList(ListNode head){
        if (head == null){
            return head;
        }
        if (head.next == null){
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;

    }

    public ListNode reverseList2(){
        if (head == null){
            return null;
        }

        //先从前两个节点下手
        ListNode cur = head.next;
        head.next = null;
        while (cur != null){
            ListNode curN = cur.next;
            cur.next = head;
            head = cur; //更改头结点
            cur = curN; //更改cur，使其继续遍历；
        }
        return head;

    }

    public ListNode head;

    public void CreList() {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;

        this.head = listNode1;

    }

    //找中间节点，并返回中间节点的地址
    public ListNode middleNode2(){
        //一个节点都没有
        if (head == null){
            return null;
        }
        //只有一个节点
        if (head.next == null){
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next!= null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //找中间节点
    public ListNode middleNode(){
        if (head == null){
            return null;
        }
        if (head.next == null){
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public void show() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }

    }

    public void show(ListNode head) {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }

    }
    // ⽆头单向⾮循环链表实现

    //头插法
    public void addFirst(int data) {

        ListNode node = new ListNode(data);
        node.next = head;
        head = node;

    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
        }
        //要利用循环来找尾巴
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;

    }
    //任意位置前面插⼊,第⼀个数据节点为0号下标

    //TODO;
    public void addIndex(int index, int data) {
        int len = size();
        if (index < 0 || index > len) {
            throw new PosBoundsOfException("插入位置错误");
        }
        //头插
        if (index == 0) {
            addFirst(data);
        }
        //尾插 0,1,2,3,4

        if (index == len) {
            addLast(data);
        }

        //中间

        ListNode cur = SearchIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;



    }

    //找到index位置的前一个节点的引用
    private ListNode SearchIndex(int index) {
        int len = size();
        if (index < 0 || index > len) {
            return null;
        }
        int count = 0;
        ListNode cur = head;
        while (count != index -1){
            cur = cur.next;
            count++;
        }
        return cur;
    }


    //查找是否包含关键字key是否在单链表当中
    public boolean contains (int key){
        ListNode cur = head;

        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第⼀次出现关键字为key的节点
    public void remove ( int key){

        if (head == null){
            return;
        }
        if (head.val == key) {
            head = head.next;
            return;
        }
        ListNode prev  = findKey(key);
        if (prev == null){
            return;
        }
        ListNode del = prev.next;
        prev.next = del.next;


    }

    //删除节点本质是更改指向,找上一个节点,并修改指向
    private ListNode findKey(int key){
        ListNode prev = head;
        //在前一个节点的位置就判断了下一个节点的val值是否等于key
        while (prev.next != null){
            if (prev.next.val == key){
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }

    //删除所有值为key的节点
    public void removeAllKey ( int key){
        if (head == null){
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;

        //注意：cur！= null，并不是cur.next != null，前者指的是下一个节点
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = prev.next;//或者prev = cur
                cur = cur.next;

            }
        }//以上代码因为cur代表要删除节点，如果头结点val == key，就导致头结点没有删除
        //删除头结点
        if (head.val == key){
            head = head.next;
        }

    }
    //得到单链表的⻓度
    public int size () {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void clear () {
        head = null;

    }
    public void display () {
    }

}
